package com.zmj.controller;

import com.zmj.common.ResponseVO;
import com.zmj.dto.req.ValidationReq;
import com.zmj.servie.HelloServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("demo")
public class HelloController {

    @Autowired
    private HelloServiceImpl helloService;

    @GetMapping("hello")
    public ResponseVO hello() {
        return ResponseVO.build("hello init-teplate");
    }

    /**
     * 全局异常demo演示
     */
    @GetMapping("exception")
    public ResponseVO exception(@RequestParam(value = "errorType", required = false) String errorType) {
        helloService.exception(errorType);
        return ResponseVO.build();
    }


    /**
     * test-过滤器请求中携带Filter的请求
     */
    @GetMapping("filter")
    public ResponseVO filter() {
        return ResponseVO.build("未被过滤器顺利执行！！");
    }


    /**
     * test-拦截请求中携带interceptor的请求
     */
    @GetMapping("interceptor")
    public ResponseVO interceptor() {
        return ResponseVO.build("未被拦截顺利执行！！");
    }


    /**
     * @Validated 属性校验
     */
    @PostMapping("validated")
    public ResponseVO validated(@Validated @RequestBody ValidationReq req) {
        return ResponseVO.build();
    }

    /**
     * 异步操作
     */
    @GetMapping("sync")
    public ResponseVO sync() {
        System.out.println(Thread.currentThread().getName() + " controller run ...");
        helloService.sync();
        return ResponseVO.build();
    }

    @GetMapping("cache/{id}")
    public ResponseVO cache(@PathVariable Integer id) {
        return ResponseVO.build(helloService.cache(id));
    }

    @GetMapping("delCache/{id}")
    public ResponseVO delCache(@PathVariable Integer id) {
        helloService.delCache(id);
        return ResponseVO.build();
    }

}

